﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

using LitJson;
using org.opensocial.client;
using org.opensocial.data;
using OpenSocialPlugin.General;

namespace OpenSocialPlugin.Providers
{
    public class BaseSite
    {
        /// <summary>
        /// Get Json data from url
        /// </summary>
        /// <param name="url"></param>
        /// <returns>JsonData array</returns>
        protected JsonData GetJson(string url)
        {
            string webData = GetWebData(url);
            try
            {
                // attempts to convert the returned string into a JsonData object
                JsonData data = JsonMapper.ToObject(webData);
                return data;
            }
            catch (JsonException e)
            {
                //Log.Error("Error parsing results from {0} as JSON: {1}", url, e.Message);
            }
            return null;
        }


        public static string GetWebData(string url)
        {
            string _data = string.Empty;

            int tryCount = 0;
            int maxRetries = 3;
            int timeout = 0;
            int timeoutIncrement = 5000;

            while (_data == string.Empty && tryCount < maxRetries)
            {
                try
                {
                    tryCount++;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Timeout = timeout + (timeoutIncrement * tryCount);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream resultData = response.GetResponseStream();
                    StreamReader reader = new StreamReader(resultData, Encoding.UTF8, true);
                    _data = reader.ReadToEnd().Replace('\0', ' ');
                    resultData.Close();
                    reader.Close();
                    response.Close();
                }
                catch (WebException ex)
                {
                    //Log.Error("{0}", ex.Message);
                    //if (tryCount == maxRetries)
                    //    Log.Error("[MyTrailers] Error connecting to {0} . Reached retry limit of {1}", url, maxRetries);
                }
            }

            return _data;
        }

        #region private
        public virtual OSP_Person To_Osd_Person(Person per)
        {
            OSP_Person person = new OSP_Person();
            person.Name = per.DisplayName;
            person.Id = per.Id;
            person.Thumb = per.ThumbnailUrl;
            if (per.hasField("photos") && per.getField("photos").getValues().Count > 1)
            {
                person.LargeThumb = per.getField("photos").getValues()[1].getStringValue("value");
            }
            else
            {
                person.LargeThumb = person.Thumb;
            }
            return person;
        }
        #endregion
    }
}
